<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MDownSharesController
extends MApplicationComponent
implements MIController
{
    private $_file_list = array();
    private $_home      = null;
    
    public function invoke($uri=null)
    {
                                parent::init();
        $key = array_slice(explode('/', $uri),2);
        $key = $key[0];
        if ($pos = strpos($key, '?'))
        {
            $key = substr($key, 0, $pos);
        }
                                $query_db_file = MShareFiles::queryShareByKey($key);
        if ($query_db_file === false || empty($query_db_file))
        {
            echo "No file was not found at the specified path";
            return ;
        }
                                if ($query_db_file[0]["expires"] - time() <= 0)
        {
            echo "Has expired";
            return ;
        }
                                $query_file_db = MFiles::queryFileById($query_db_file[0]["file_id"]);
        if ($query_file_db === false || empty($query_file_db))
        {
            echo "No file was not found at the specified path";
            return ;
        }
                                if ($query_file_db[0]["file_type"] == MConst::OBJECT_TYPE_FILE)
        {
            $this->handlerFile($query_file_db[0]["version_id"], 
                               $query_file_db[0]["file_name"], 
                                $file_version[0]["mime_type"]);
            return ;
        }
                                $this->_home    = $query_file_db[0]["file_path"];
        $file_name      = $query_file_db[0]["file_name"].".zip";
                                if (!file_exists(BASE_TEM))
        {
            MUtils::MkDirs(BASE_TEM);
        }
                                $tmp_path = BASE_TEM."{$query_file_db[0]["user_id"]}/{$query_file_db[0]["id"]}/";
        if (!file_exists($tmp_path))
        {
            MUtils::MkDirs($tmp_path);
        }
        $zip_file       = $tmp_path.$file_name;
        $mime_type      = "application/zip";
        if (!file_exists($zip_file))
        {
                                                file_put_contents($zip_file, "");
            if ($this->handlerDirectory($query_file_db[0]["id"]) === false)
            {
                echo "zip directory error";
                return ;
            }
                                                if ($this->zipFile($zip_file) === false)
            {
                echo "zip file error";
                return ;
            }
        }
        $this->output($zip_file, $file_name, $mime_type);
    }
    
    
    private function output($file_path, $file_name, $mime_type)
    {
                                if (headers_sent()) {
            exit;
        }
        $file_get = new MFileGetController();
        $file_get->download_normal($file_path, 
                                $file_name, $mime_type);
    }
    
    
    private function handlerFile($version_id, $file_name, $mime_type)
    {
        $file_version = MFileVersions::queryFileVersionByID($version_id);
        if ($file_version === false || empty($file_version)) {
            echo "No file was not found at the specified path";
            return ;
        }
                                $signature = $file_version[0]["file_signature"];
        $file_path = DOCUMENT_ROOT_BLOCK . MUtils::getPathBySplitStr($signature);
        if (file_exists($file_path) === false) {
            echo "No file was not found at the specified path";
            return ;
        }
        $this->output($file_path, $file_name, $mime_type);
    }
    
    
    private function handlerDirectory($parent_file_id)
    {
                                $query_children_files = MFiles::queryChildrenFilesByParentFileID($parent_file_id);
        if ($query_children_files === false)
        {
            return false;
        }
        if (empty($query_children_files))
        {
            return ;
        }
        foreach ($query_children_files as $key => $children_file)
        {
            $file            = new MFiles();
            $file->file_type = $children_file["file_type"];
                                                $to_file_path           = $this->getToPath($children_file["file_path"]);
            $file->to_file_path     = $to_file_path;
            if ($children_file["file_type"] == MConst::OBJECT_TYPE_FILE)
            {
                                                                $file_version = MFileVersions::queryFileVersionByID($children_file["version_id"]);                   if ($file_version === false || empty($file_version)) {
                    continue;
                }
                $signature = $file_version[0]["file_signature"];
                $from_file_path = DOCUMENT_ROOT_BLOCK . MUtils::getPathBySplitStr($signature);
                
                $file->from_file_path = $from_file_path;
                
                array_push($this->_file_list, $file);
                continue;
            }
            array_push($this->_file_list, $file);
                                                $this->handlerDirectory($children_file["id"]);
        }
    }
    
    
    private function getToPath($path)
    {
        $index      = strlen($this->_home)-1;
        $file_path  = substr_replace($path, "", 0, $index);
        return $file_path;
    }
    
    
    private function zipFile($zip_file)
    {
        $zip = new ZipArchive;
        if ($zip->open($zip_file) === false) {
            return false;
        }
        foreach ($this->_file_list as $file)
        {
            if ($file->file_type == MConst::OBJECT_TYPE_DIRECTORY)
            {
                $zip->addEmptyDir($file->to_file_path);
                continue;
            }
            $zip->addFile($file->from_file_path, $file->to_file_path);
        }
        $zip->close();
        return true;
    }

}
?>